package com.thiny.android.app.tnote.ui;

import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.thiny.android.app.tnote.Constants;
import com.thiny.android.app.tnote.R;
import com.thiny.android.app.tnote.adapter.NoteAdapter;
import com.thiny.android.app.tnote.datebase.SQLiteHelper;
import com.thiny.android.app.tnote.model.Note;
import com.thiny.android.app.tnote.util.TimeUtil;

public class MainActivity extends BaseActivity implements OnClickListener {

	// 当月的Calendar
	private Calendar mCalendar;
	private ListView mListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		initListView();
		mCalendar = Calendar.getInstance();
		mCalendar.set(Calendar.MILLISECOND, 0);
		mCalendar.set(Calendar.DAY_OF_MONTH, 1);
		mCalendar.set(Calendar.HOUR_OF_DAY, 0);
		mCalendar.set(Calendar.MINUTE, 0);
		mCalendar.set(Calendar.SECOND, 0);
		updateMonthText();
		findViewById(R.id.pre_month).setOnClickListener(this);
		findViewById(R.id.next_month).setOnClickListener(this);
	}

	private void initListView() {
		mListView = (ListView) findViewById(R.id.note_list);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				Note note = (Note) adapterView.getItemAtPosition(position);
				if (note == null) {
					Note n = new Note();
					n.setTitle(position + "");
					mCalendar.set(Calendar.DAY_OF_MONTH, position + 1);
					n.setDate(mCalendar.getTimeInMillis());
					Intent intent = new Intent(MainActivity.this,
							SettingActivity.class);
					intent.putExtra(Constants.EXTRA_NOTE, n);
					startActivity(intent);
				} else {
					Intent intent = new Intent(MainActivity.this,
							DetailActivity.class);
					intent.putExtra(Constants.EXTRA_NOTE, note);
					startActivity(intent);
				}
			}
		});
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> adapterView,
					View view, int position, long id) {
				Note note = (Note) adapterView.getItemAtPosition(position);
				if (note == null)
					return false;
				Intent intent = new Intent(MainActivity.this,
						SettingActivity.class);
				intent.putExtra(Constants.EXTRA_NOTE, note);
				startActivity(intent);
				return true;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadListView();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pre_month:
			loadPreMonth();
			break;

		case R.id.next_month:
			loadNextMonth();
			break;
		}
		updateMonthText();
		loadListView();
		// startActivity(new Intent(this, SettingActivity.class));
	}

	// 先设置到上月，在将日改为“1”
	private void loadPreMonth() {
		long pre = mCalendar.getTimeInMillis() - 10 * TimeUtil.DAY_MILLISECOND;
		mCalendar.setTimeInMillis(pre);
		mCalendar.set(Calendar.DAY_OF_MONTH, 1);
	}

	// 先设置到下月，在将日改为“1”
	private void loadNextMonth() {
		long next = mCalendar.getTimeInMillis() + TimeUtil.DAY_MILLISECOND
				* mCalendar.getMaximum(Calendar.DAY_OF_MONTH);
		mCalendar.setTimeInMillis(next);
		// mCalendar.set(Calendar.DAY_OF_MONTH,
		// mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1);
	}

	private void updateMonthText() {
		TextView tv = (TextView) findViewById(R.id.month_tv);
		tv.setText(DateFormat.format("yyyy年M月", mCalendar));
	}

	private void loadListView() {
		new AsyncTask<Void, Void, SparseArray<Note>>() {

			@Override
			protected SparseArray<Note> doInBackground(Void... params) {
				List<Note> list = SQLiteHelper.getInstance(MainActivity.this)
						.findAllInMonth(mCalendar.getTimeInMillis());
				SparseArray<Note> notes = new SparseArray<Note>();
				Calendar calendar = Calendar.getInstance();
				for (Note note : list) {
					calendar.setTimeInMillis(note.getDate());
					int day = calendar.get(Calendar.DAY_OF_MONTH);
					notes.put(day, note);
				}
				int max = maxDay();
				for (int i = 1; i <= max; i++) {
					if (notes.get(i) == null) {
						notes.put(i, null);
					}
				}
				return notes;
			}

			@Override
			protected void onPostExecute(SparseArray<Note> notes) {
				mListView.setAdapter(new NoteAdapter(MainActivity.this, notes));
			}

		}.execute();

	}

	private int maxDay() {
		int maxDay = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		return maxDay;
	}
}
